﻿/*
 * 由SharpDevelop创建。
 * 用户： xieen
 * 日期: 2013-08-21
 * 时间: 15:06
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;

namespace GoF23Learning._19.Interpreter_Pattern.Refactor
{
	/// <summary>
	/// Description of Expression.
	/// </summary>
	public abstract class Expression
	{
		protected Dictionary<string,int>
			dic = new Dictionary<string, int>();
		
		public Expression(){		
			//dic.Add("零",0);
			dic.Add("一",1);
			dic.Add("二",2);
			dic.Add("三",3);
			dic.Add("四",4);
			dic.Add("五",5);
			dic.Add("六",6);
			dic.Add("七",7);
			dic.Add("八",8);
			dic.Add("九",9);			
		}
		
		
		public abstract string GetPostfix();
		public abstract int Multiplier();
		public virtual int GetLength()
		{
			return this.GetPostfix().Length + 1;
		}
		
		public virtual void Interpreter(Context context)
		{
			int length = context.Statement.Length;
			if( length==0)
				return ;
			int subLength=0;
			
			foreach (string key in dic.Keys) {
				int value =dic[key];
				string suffix = key+this.GetPostfix();
				if(context.Statement.EndsWith(suffix))
				{
					context.Data += value * this.Multiplier();
					subLength = length-this.GetLength();
					context.Statement = context.Statement.Substring(0,subLength);
				}
				if(context.Statement.EndsWith("零"))
				{
					subLength = length-1;
					context.Statement = context.Statement.Substring(0,subLength);
				}
			}
		}
		
		
		
	}
}
